"
SUnit tests for ByteArray
"
Class {
	#name : 'ByteArrayTest',
	#superclass : 'TestCase',
	#traits : 'TSequenceableTruncatedToSize',
	#classTraits : 'TSequenceableTruncatedToSize classTrait',
	#instVars : [
		'collectionWith5Elements'
	],
	#category : 'Collections-Native-Tests-Base',
	#package : 'Collections-Native-Tests',
	#tag : 'Base'
}

{ #category : 'requirements' }
ByteArrayTest >> collectionWith5Elements [

	^ collectionWith5Elements
]

{ #category : 'running' }
ByteArrayTest >> setUp [

	super setUp.
	collectionWith5Elements := #[1 2 3 5 4]
]

{ #category : 'tests' }
ByteArrayTest >> testAsByteArrayOfSize [
	self assert: (#[51 52 53] asByteArrayOfSize: 10 ) equals: #[0 0 0 0 0 0 0 51 52 53].
	self assert: (#[51 52 53] asByteArrayOfSize: 3 ) equals: #[51 52 53]
]

{ #category : 'tests' }
ByteArrayTest >> testAsInteger [ 
	self assert: #[122] asInteger equals: 122.
	self assert: #[122 8] asInteger equals: 31240.
	self assert: #[0] asInteger equals: 0.
	self assert: #[255] asInteger equals: 255.
	self assert: #[] asInteger equals: 0
]

{ #category : 'tests' }
ByteArrayTest >> testBooleanAt [
	"Checking the result of ByteArray>>#booleanAt on a few instances of ByteArray. It should return false on 0-valued bytes and true on all other bytes (according to the integer to boolean C convention)."

	self deny: (#[0] booleanAt: 1).
	self assert: (#[1] booleanAt: 1).
	self assert: (#[255 2 3] booleanAt: 1).
	self deny: (#[0 25] booleanAt: 1).
	self assert: (#[255] booleanAt: 1)
]

{ #category : 'tests' }
ByteArrayTest >> testFallbackReplaceFromToWithForString [
	| testString byteArray stringByteSize |
	testString := 'Test string'.
	stringByteSize := 'Test string' byteSize.
	byteArray := ByteArray new: stringByteSize.

	self
		shouldnt: [
			byteArray
				replaceFrom: 1
				to: stringByteSize
				with: testString
				startingAt: 1 ]
		raise: Exception
		description: 'Primitive 105 should be optional for ByteArray'
]

{ #category : 'tests' }
ByteArrayTest >> testFallbackReplaceFromToWithForWideString [
	| testString byteArray stringByteSize |
	testString := 'Test string' asWideString.
	stringByteSize := 'Test string' byteSize.
	byteArray := ByteArray new: stringByteSize.

	self
		shouldnt: [
			byteArray
				replaceFrom: 1
				to: stringByteSize
				with: testString
				startingAt: 1 ]
		raise: Exception
		description: 'Primitive 105 should be optional for ByteArray'
]

{ #category : 'tests' }
ByteArrayTest >> testFourthByteArraysReturnTheCorrectValues [

	self assert: (#[16r3F 16r80 0 0]  floatAt:1 bigEndian: true) equals: 1.0.
	self assert: (#[16rC0 0 0 0]  floatAt:1 bigEndian: true) equals: -2.0
]

{ #category : 'tests' }
ByteArrayTest >> testHex [
	self assert: #[122 43 213 7] hex equals: '7a2bd507'.
	self
		assert: #[151 193 242 221 249 32 153 72 179 41 49 154 48 193 99 134] hex
		equals: '97c1f2ddf9209948b329319a30c16386'.
	self
		assert: (ByteArray readHexFrom: '7A2BD507')
		equals: #[122 43 213 7].
	self
		assert: (ByteArray readHexFrom: '7a2bd507')
		equals: #[122 43 213 7]
]

{ #category : 'tests' }
ByteArrayTest >> testHexDumponmax [

	| source display expected |

	source := '01234567890123456789'.
	display := String streamContents: [ :stream |
		source asByteArray hexDumpOn: stream max: 1000 ].
	expected := '00000000  30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35   |0123456789012345|
00000010  36 37 38 39                                       |6789            |
'.
	self assert: display equals: expected
]

{ #category : 'tests' }
ByteArrayTest >> testindexOfStartingAt [
	|array|
	array := #[1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 0 0].
	self assert: (array indexOf: 1 startingAt: 10) equals: 12.
	self assert: (array indexOf: 17 startingAt: 10) equals: 0
]
